package com.hung.dao.daoImpl;

import com.hung.dao.PageDao;
import com.hung.pojo.Matter;
import com.hung.util.jdbc.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Hung
 */
public class PageDaoImpl implements PageDao {
    /**
     * 查询对应瓜圈的所有事件
     *
     * @param pid
     * @param condition
     * @param start
     * @param rows
     * @return
     */
    @Override
    public List<Matter> queryMatters(Integer pid, String condition, Integer start, Integer rows) {
        List<Matter> matters = new ArrayList<>();
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection = JdbcUtil.getConnection();
            String sql="select * from matter where pid=?";

            StringBuilder sb=new StringBuilder(sql);
            if (condition!=null && !"".equals(condition)){
                sb.append(" and content like '%"+condition+"%'");
            }
            sb.append(" limit ?,? ");

            //传入参数,执行sql对象
            preparedStatement = connection.prepareStatement(sb.toString());
            preparedStatement.setInt(1,pid);
            preparedStatement.setInt(2,start);
            preparedStatement.setInt(3,rows);

            //获取resultSet中的数据保存到list中
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Matter matter = new Matter();
                matter.setId(resultSet.getInt("id"));
                matter.setPid(resultSet.getInt("pid"));
                matter.setUid(resultSet.getInt("uid"));
                matter.setPostTime(resultSet.getString("postTime"));
                matter.setContent(resultSet.getString("content"));
                matter.setPraise(resultSet.getString("praise"));
                matter.setCommentId(resultSet.getInt("commentId"));
                matters.add(matter);
            }
            return matters;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtil.close(preparedStatement,connection);
        }
        return matters;
    }

    /**
     * 根据条件查询总页数
     *
     * @param condition
     * @return
     */
    @Override
    public Integer queryTotalCount(String condition) {
        int count=0;
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        try {
            connection = JdbcUtil.getConnection();
            //遍历条件查询语句
            String sql="select count(*) from matter where 1 =1";
            StringBuilder sb=new StringBuilder(sql);

            if (condition!=null && !"".equals(condition)){
                sb.append(" and content like '%"+condition+"%'");
            }

            //执行sql对象
            preparedStatement = connection.prepareStatement(sb.toString());
            ResultSet resultSet = preparedStatement.executeQuery();
            //获取总记录数
            while (resultSet.next()){
                count= resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JdbcUtil.close(preparedStatement,connection);
        }
        return count;
    }
}
